package com.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.web.entity.Token;
import com.web.entity.UserInfo;
import com.web.mapper.UserInfoMapper;
import com.web.param.AdminLoginDto;
import com.web.param.InsertUserDto;
import com.web.param.UpdatePasswordDto;
import com.web.service.ITokenService;
import com.web.service.IUserInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.web.util.LoginException;
import com.web.util.ServiceException;
import com.web.util.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hgjxyz
 * @since 2020-11-18
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Autowired private ITokenService tokenService;

    @Override
    public void addUser(InsertUserDto dto) throws ServiceException, LoginException {
        Token token = this.tokenService.getByToken(dto.getToken());
        if(token.getTheType() == 0)throw new ServiceException("权限不足");
        Wrapper<UserInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("username", dto.getUsername()).eq("is_delete", 0);
        if(this.selectCount(wrapper) > 0) throw new ServiceException("这个用户名已经存在了");
        UserInfo userInfo = new UserInfo();
        userInfo.setId(UUID.randomUUID().toString());
        userInfo.setUsername(dto.getUsername());
        userInfo.setPassword(DigestUtils.md5DigestAsHex(dto.getPassword().getBytes()));
        userInfo.setAvatar(dto.getAvatar());
        userInfo.setPhone(dto.getPhone());
        userInfo.setIsEnable(0);
        userInfo.setIsDelete(0);
        userInfo.setCreatedTime(new Date());
        if(!this.insert(userInfo))throw new ServiceException("新增失败");
    }

    @Override
    public UserInfo login(AdminLoginDto dto) throws ServiceException {
        Wrapper<UserInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("username", dto.getUsername()).eq("is_enable", 0).eq("is_delete", 0);
        UserInfo userInfo = this.selectOne(wrapper);
        if(userInfo == null) throw new ServiceException("账号不存在或已被禁用");
        String password = DigestUtils.md5DigestAsHex(dto.getPassword().getBytes());
        if(!userInfo.getPassword().equals(password))throw new ServiceException("密码错误");
        userInfo.setPassword(this.tokenService.addToken(userInfo.getId(), 0));
        return userInfo;
    }

    @Override
    public void updatePassword(UpdatePasswordDto dto) throws ServiceException, LoginException {
        Token token = this.tokenService.getByToken(dto.getToken());
        if(token.getTheType() == 1)throw new ServiceException("权限不足");
        UserInfo userInfo = this.selectById(token.getId());
        if(userInfo == null || userInfo.getIsEnable() == 1 || userInfo.getIsDelete() == 1)
            throw new ServiceException("账号不存在或已被禁用");
        String oldPwd = DigestUtils.md5DigestAsHex(dto.getOldPwd().getBytes());
        if(!userInfo.getPassword().equals(oldPwd))throw new ServiceException("旧密码不正确");
        userInfo.setPassword(DigestUtils.md5DigestAsHex(dto.getNewPwd().getBytes()));
        if(!this.updateById(userInfo))throw new ServiceException("修改失败");
    }
}
